package com.xinlu.common.game.dao;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.xinlu.common.game.value.TzjlVo;
import com.xinlu.derek.common.BaseDao;
import com.xinlu.persistence.Tzjl;
import com.xinlu.util.tool.PageObject;

public class TzjlDao extends BaseDao {

	/**
	 * 
	 * Function : 添加投注记录信息
	 * 
	 * @param instance
	 *            Derek 2011-5-14 下午09:40:16
	 */
	public Long save(Tzjl instance) {

		return (Long) super.save(instance);
	}

	public void update(Tzjl instance) {

		super.update(instance);
	}

	/**
	 * 加载投注记录
	 * 
	 * @param caiZhong
	 * @return
	 */
	public PageObject loadTzjls(String yhm, String caiZhong, String bgnDate, String endDate, int pageNo) {
		String cmdText = " select t.*,w.wfjczwm from Tzjl as t inner join Wflx as w on w.wfjc=t.yxlx and t.yhm='" + yhm + "' and dgsj>='" + bgnDate + "' and dgsj<='" + endDate + "' ";
		if (!"--全部--".equals(caiZhong)) {
			cmdText += " and t.yxlx like '" + caiZhong + "%'";
		}
		cmdText+=" order by t.id desc ";

		return super.findPageObjectByProc(10, pageNo, cmdText, TzjlVo.class);
	}

	/**
	 * 加载追号记录
	 * 
	 * @param caiZhong
	 * @param zhzt
	 * @return
	 */
	public PageObject loadZhjls(String caiZhong, String zhzt, String bgnDate, String endDate,int pageNo) {

		String cmdText = " select t.*,w.wfjczwm from Tzjl as t inner join Wflx as w on w.wfjc=t.yxlx and t.zjzt<>'已撤单' and t.dzzt<>'已撤单' and t.dgsj>='" + bgnDate + "' and t.dgsj<='" + endDate + "' ";
		if (!"--全部--".equals(caiZhong)) {
			cmdText += " and t.yxlx like '" + caiZhong + "%'";
		}
		if (!"--全部--".equals(zhzt)) {
			cmdText += " and t.sffh=" + zhzt;
		}
		cmdText+=" order by t.id desc ";

		return super.findPageObjectByProc(10, pageNo, cmdText, TzjlVo.class);
	}

	/**
	 * 加载方案管理
	 * 
	 * @param caiZhong
	 * @param bgnDate
	 * @param endDate
	 * @param dzh
	 * @param qh
	 * @param yhms
	 *            用户列表，包含当前用户跟下级用户集合
	 * @param zhm
	 * @return
	 */
	public PageObject loadFagls(String caiZhong, String bgnDate, String endDate, String dzh, String qh, List yhms, String zhm, int pageNo) {

		String cmdText = " select t.*,w.wfjczwm from Tzjl t, Wflx w where w.wfjc=t.yxlx and t.dgsj>='" + bgnDate + "' and t.dgsj<='" + endDate + "' ";
		if (!"--全部--".equals(caiZhong)) {
			cmdText += " and t.yxlx like '" + caiZhong + "%'";
		}
		if (StringUtils.isNotEmpty(dzh)) {
			cmdText += " and t.dzh = '" + dzh + "' ";
		}
		if (StringUtils.isNotEmpty(qh)) {
			cmdText += " and t.qh = '" + qh + "' ";
		}
		if (StringUtils.isNotEmpty(zhm)) {
			cmdText += " and t.zhm = " + zhm + " ";
		}

		List<String> yhmsCondition = new ArrayList<String>();
		for (int i = 0; i < yhms.size(); i++) {
			yhmsCondition.add(" t.yhm='" + yhms.get(i) + "' ");
		}

		cmdText += " and ( " + StringUtils.join(yhmsCondition.toArray(), "or") + " )";

		return super.findPageObjectByProc(10, pageNo, cmdText, TzjlVo.class);
	}

	/**
	 * 根据id加载投注记录
	 * 
	 * @param id
	 * @return
	 */
	public Tzjl loadTzjl(int id) {
		return (Tzjl) super.load(Tzjl.class, id);
	}

	/**
	 * 
	 * Function : 查询投注记录明细表(资金明细表)
	 * 
	 * @param yxlx
	 * @param startdate
	 * @param enddate
	 * @param username
	 * @return Derek 2011-5-29 下午08:17:32
	 */
	public List findAllZjmxList(String yxlx, Date startdate, Date enddate, String username) {

		String hql = " select wf.wfjczwm ,bb from Wflx wf, BankBack bb " + " where bb.username = ? " + " and wf.wfjc like ? " +
		// " and bb.addtime between ? and ? " +
				" and bb.type = wf.wfjc";

		return super.find(hql, new Object[] { username, yxlx });
	}

	/**
	 * 
	 * Function : 获取存取款明细
	 * 
	 * @param type
	 * @param startdate
	 * @param enddate
	 * @param username
	 * @return Derek 2011-5-29 下午08:46:10
	 */
	public List findAllCkqkList(String type, Date startdate, Date enddate, String username) {

		String hql = " from BankBack bb where bb.type = ? and bb.addtime between ? and ? and username = ? ";

		return super.find(hql, new Object[] { type, startdate, enddate, username });

	}

	/**
	 * 
	 * Function : 查询投注记录明细表(资金明细表)
	 * 
	 * @param yxlx
	 * @param startdate
	 * @param enddate
	 * @param username
	 * @return Derek 2011-5-29 下午08:17:32
	 */
	public List findAllTzmxList(String yxlx, String username) {

		String hql = " select wf.wfjczwm ,bb from Wflx wf, BankBack bb " + " where bb.username = ? " + " and wf.wfjc like ? " +
		// " and bb.addtime between ? and ? " +
				" and bb.type = wf.wfjc";

		return super.find(hql, new Object[] { username, yxlx });
	}

	/**
	 * 
	 * Function : 获取追号记录详细信息
	 * 
	 * @param yxlx
	 * @return Derek 2011-5-30 上午01:15:25
	 */
	public List findAllZhjlDetailList(String yxlx) {

		String hql = " select tz, wf.wfjczwm from Tzjl tz, Wfjc wf where tz.id = tz.zhm and sfzh = 1 and tz.yxlx = ? and tz.yxlx = wf.wfjc ";

		return super.find(hql, yxlx);
	}

	/**
	 * 获取完成期数
	 * 
	 * @param id
	 * @return
	 */
	public String getWcqs(Long id) {
		String cmdText = "from Tzjl where zhm=" + id + " and sffh=1 and zjzt<>'未开奖' and zjzt<>'已撤单' and dzzt='完成' ";
		List rst = super.find(cmdText);
		if (rst != null) {
			return rst.size() + "";
		}
		return "0";
	}

	/**
	 * 加载投注记录
	 * 
	 * @param dzh
	 * @return
	 */
	public List loadTzmx(String dzh) {
		return super.find("select t, w.wfjczwm from Tzjl t,Wflx w where w.wfjc=t.yxlx and dzh='" + dzh + "'");
	}
	
	
	/**
	 * 
	 * Function :		获取今日投注列表
	 * @param czlx		彩种类型
	 * @param username	用户名
	 * @param today		日期
	 * @return
	 * Derek
	 * 2011-6-10 下午09:59:48
	 */
	public List getJrtzList(String czlx, String username, Timestamp today){
		
		String hql = " select tz, wf.wfjczwm from Tzjl tz, Wflx wf where tz.yhm = ? and tz.dgsj > ? and tz.yxlx like ? and tz.yxlx = wf.wfjc order by tz.dgsj desc ";
		
		return super.find(hql, new Object[]{username, today, czlx});
	}
}
